<?php

class Controller extends MVCBase
{
    public $models = NULL;
    public $views = NULL;
    
    function  __construct()
    {
        parent::__construct();
        
    }

    public function loadModel($modelName,$forceLoadGeneral = FALSE)
    {
        $modelName = strtolower($modelName);
        $modelClass = $modelName . 'Model';
        if($forceLoadGeneral)
        {
            $path = APP_PATH . 'models' . DS . $modelName . '.model.php';
        }
        else
        {
            global $_app_config;
            if(isset($_app_config['component_enable']) && $_app_config['component_enable'] == TRUE)
            {
                $path = APP_PATH . 'components' . DS . $_app_config['component_name'] . DS . 'models' . DS . $modelName .'.model.php';
            }
            else
            {
                $path = APP_PATH . 'models' . DS . $modelName . '.model.php';
            }
        }
        if(!$this->loadClass($modelClass,$path,FALSE))
        {
            exit("Model $modelName does not exist !");
        }
        $modelObj = new $modelClass;
        $this->register('models',$modelObj,$modelName);
        return $modelObj;
    }

    protected function loadView($viewName, $forceLoadGeneral = FALSE)
    {
        $viewName = strtolower($viewName);
        if ($forceLoadGeneral)
        {
            $path = APP_PATH . 'views' . DS . $viewName . '.view.php';
        }
        else
        {
            global $_app_config;
            if (isset($_app_config['component_enable']) && $_app_config['component_enable'] == TRUE)
            {
                $path = APP_PATH . 'components' . DS . $_app_config['component_name'] . DS . 'views' . DS . $viewName . '.view.php';
            }
            else
            {
                $path = APP_PATH . 'views' . DS . $viewName . '.view.php';
            }
        }
        if (file_exists($path))
        {
            require $path;
            $viewClass = $viewName . 'View';
            if (class_exists($viewClass))
            {
                $obj = new $viewClass;
            }
        }
        if (!isset($obj))
            $obj = new View($viewName);
        $this->register('views', $obj, $viewName);
        return $obj;
    }
}

?>
